home *** CD-ROM | disk | FTP | other *** search
- /*
- * xptstats.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /*
- * XPTSTATS
- *
- * Asses the degree of randomness in a point pattern
- * by drawing and filling circles of increasing radius
- * whose centers lie at the specified points.
- *
- * input:
- * A .vin file (generated by spp)
- *
- * output:
- * a
- */
-
- #include "xptstats.h"
-
- /* globals */
- extern char *optarg;
- extern int optind, opterr;
- extern short tiffInput; /* flag=0 if no ImageIn to set tags; else =1 */
-
- /*
- * readpoints()
- * DESCRIPTION:
- * read points from file
- * ARGUMENTS:
- * filename: string for file to read from
- * RETURN VALUE:
- * none
- */
-
- void
- readpoints (char *filename, int *npoints, struct Point **points, float *xmin, float *xmax, float *ymin, float *ymax)
- {
- int i;
- char buf[1024];
- char *strptr;
- FILE *file;
-
- strcpy (buf, filename);
- if (((strptr = strstr (buf, FILE_EXT)) && (strptr != (buf + strlen (buf) - strlen (FILE_EXT)))) || !strptr)
- strcat (buf, FILE_EXT);
- if ((file = fopen (buf, "r")) == NULL) {
- printf ("couldn't open file %s!!\n", buf);
- exit (1);
- }
- /*
- * skip top line in .vin data file
- */
- fscanf (file, "%d %f %f %f %f", npoints, xmin, xmax, ymin, ymax);
-
- /*
- * read data
- */
- *points = (struct Point *) calloc (*npoints, sizeof (struct Point));
- for (i = 0; i < *npoints; i++)
- fscanf (file, "%f %f", &(*points + i)->x, &(*points + i)->y);
-
-
- #ifdef SHOW_DATA
- printf ("\n...points read in are:\n");
- printf ("...parameters: npoints: %4d", *npoints);
- printf ("\n extrema: %f %f %f %f\n", *xmin, *xmax, *ymin, *ymax);
-
- for (i = 0; i < *npoints; i += 1) {
- printf (" x[%d] = %f y[%d] = %f\n",
- i, (*points + i)->x, i, (*points + i)->y);
- }
- #endif
- fclose (file);
- }
-
-
- void
- exit_messg (str, code)
- char *str;
- int code;
- {
- printf ("\n%s", str);
- exit (code);
- }
-
- /*
- * usage of routine
- */
- void
- usage (char *progname)
- {
- progname = last_bs (progname);
- printf ("USAGE: %s infile [-t t_max] [-n ns_max] [-w file] [-L]\n", progname);
- printf ("\n%s constructs the distribution functions N(t)/N_0\n");
- printf ("and P(t) for a given point pattern\n\n");
- printf ("ARGUMENTS:\n");
- printf (" infile: input filename (.vin) (ASCII)\n");
- printf (" produced by spp or xah\n\n");
- printf ("OPTIONS:\n");
- printf (" -t t_max: maximum radius (R/R0) at which to stop (default = %d)\n", T_MAX_DEF);
- printf (" -n ns_max: maximum # iteration steps (default = %d)\n", NS_MAX_DEF);
- printf (" -w file: write data to file\n");
- printf (" -L: print Software License for this module\n");
- exit (1);
- }
-
-
- void
- main (int argc, char *argv[])
- {
- int i;
- int i_arg;
- int ns;
- int ns_max = NS_MAX_DEF;
- int nx;
- int ny;
- int S0;
- int N0;
- float t;
- float t_max = (float) T_MAX_DEF;
- float xmin;
- float xmax;
- float ymin;
- float ymax;
- float R;
- float R0;
- float DelR;
- float *t_arr;
- float *ncr_arr;
- float *p_arr;
- int fill_index;
- int circle_index;
- int n;
- long n_covered;
- struct Point *points;
- int npoints;
- Image *imgOut;
- char outFile[256];
- int write_flag = OFF;
- FILE *fpOut;
-
- /*
- * cmd line options
- */
- static char *optstring = "t:n:w:L";
-
- /*
- * parse command line
- */
- optind = 2;
- opterr = ON; /* give error messages */
-
- if (argc < 2)
- usage (argv[0]);
-
- while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
- switch (i_arg) {
- case 't':
- sscanf (optarg, "%f", &t_max);
- break;
- case 'n':
- sscanf (optarg, "%d", &ns_max);
- break;
- case 'L':
- print_sos_lic ();
- exit (0);
- case 'w':
- strcpy (outFile, optarg);
- write_flag = ON;
- break;
- default:
- printf ("...unknown command line arg encountered\n");
- exit (1);
- break;
- }
- }
- /* reset tiffInput so that we write a grayscale file (i.e tags are not copied) */
- tiffInput = 0;
-
- readpoints (argv[1], &npoints, &points, &xmin, &xmax, &ymin, &ymax);
-
- S0 = (int) ((xmax - xmin) * (ymax - ymin));
- N0 = npoints;
- fill_index = 127;
- circle_index = 255;
- R0 = (float) (sqrt ((float) S0 / (float) N0));
- R = (float) 0.0;
- DelR = R0 / (float) ns_max;
-
- printf ("Parameters are:\n");
- printf ("t_max = %f\n", t_max);
- printf ("S0 = %d, N0 = %d, R0 = %f, ns_max = %d, DelR = %f\n", S0, N0, R0, ns_max, DelR);
-
-
- t_arr = (float *) calloc (ns_max, sizeof (float));
- ncr_arr = (float *) calloc (ns_max, sizeof (float));
- p_arr = (float *) calloc (ns_max, sizeof (float));
-
- /*
- * initialize graphics
- * (allocate enough image area for drawing filled circles!!)
- */
- imgOut = ImageAlloc ((long) (ymax + ymin), (long) (xmax + xmin), BPS);
-
- ns = 0;
- while ((t = R / R0) < t_max) {
- reset_image (imgOut, 0);
- R += DelR;
- for (i = 0; i < N0; i++) {
- draw_filled_circle ((int) points[i].x, (int) points[i].y, (int) R, imgOut, circle_index);
- }
-
- n = 0;
- n_covered = 0;
- for (i = 0; i < N0; i++) {
- if (getpixel ((int) points[i].x, (int) points[i].y, imgOut) == circle_index) {
- gdImageFill ((int) points[i].x, (int) points[i].y, imgOut, fill_index);
- n++;
- }
- }
- n_covered = 0;
- for (nx = (int) xmin; nx < (int) xmax; nx++) {
- for (ny = (int) ymin; ny < (int) ymax; ny++) {
- if (getpixel (nx, ny, imgOut))
- n_covered++;
- }
- }
- t_arr[ns] = R / R0;
- ncr_arr[ns] = (float) n / (float) N0;
- p_arr[ns] = (float) n_covered / (float) S0;
- if (ns++ >= ns_max)
- break;
- }
-
-
- printf ("\nThe distribution functions are:\n\n");
- printf (" T N(T)/N0 P(T)\n");
- for (i = 0; i < ns; i++)
- printf ("%f %f %f\n", t_arr[i], ncr_arr[i], p_arr[i]);
-
- if (write_flag) {
- if ((fpOut = fopen (outFile, "w")) == NULL) {
- printf ("\n...cannot open %s for writing\n", outFile);
- exit (1);
- }
- printf ("\n...writing t, N(t)/N_0 and P(t) to %s\n", outFile);
- for (i = 0; i < ns; i++)
- fprintf (fpOut, "%f %f %f\n", t_arr[i], ncr_arr[i], p_arr[i]);
- fclose (fpOut);
- }
- ImageFree (imgOut);
-
- }
-